/**
 * 
 */
package systole.view.crud.patient.patientComponents.sports;

import java.awt.Frame;
import java.util.Iterator;
import java.util.List;
import javax.swing.JDialog;
import systole.domain.clinicalInformation.Sport;
import systole.domain.clinicalInformation.SportPatient;
import systole.domain.clinicalInformation.SportPatientId;

import systole.domain.persons.Patient;
import systole.exceptions.ExceptionDAO;
import systole.view.crud.patient.patientComponents.controller.ControllerEditionPatienComponent;

/**
 * @author jmj
 *
 */
public class ControllerEditionSportByPatient extends ControllerEditionPatienComponent {

    /**
     * @param patient
     */
    public ControllerEditionSportByPatient(Patient patient) {
        super(patient);
        this.entityName = "Actividad Física";
        this.newDescription = "Nueva";
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#loadEntityFromForm()
     */
    @Override
    protected void loadEntityFromForm() {
        SportPatient sportPatient = (SportPatient) this.curretnEntity;
        JDialogSportByPatient form = (JDialogSportByPatient) this.editForm;
        sportPatient.setSport((Sport) form.getCmbSport().getSelectedItem());
        sportPatient.setHoursXWeek((Integer) form.getjSpnHours().getValue());
        Integer age = (Integer) form.getjSpnYears().getValue();
        sportPatient.setAgeAtStartAsInteger(age);
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#loadEntityOnForm()
     */
    @Override
    protected void loadEntityOnForm() {
        SportPatient sportPatient = (SportPatient) this.curretnEntity;
        JDialogSportByPatient form = (JDialogSportByPatient) this.editForm;
        form.getCmbSport().setSelectedItem(sportPatient.getSport());
        form.getjSpnHours().setValue((sportPatient.getHoursXWeek() == null) ? 0 : sportPatient.getHoursXWeek());      
        form.getjSpnYears().setValue(sportPatient.getAgeAtStartAsInteger());
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#save()
     */
    @Override
    protected void save() {
        if (!this.isEditing()) {
            SportPatient sportPatient = (SportPatient) this.curretnEntity;
            sportPatient.setId(new SportPatientId(sportPatient.getSport().getId(), sportPatient.getPatient().getId()));
            this.patient.getSportsPatient().add(sportPatient);
        }
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#valid()
     */
    @Override
    protected String valid() {
        JDialogSportByPatient form = (JDialogSportByPatient) this.editForm;

        if ((!this.isEditing()) && ((form.getCmbSport().getSelectedIndex() == -1) || (form.getCmbSport().getSelectedItem() == null))) {
            return "Debe seleccionar una actividad física";
        }

        if ((form.getjSpnHours().getValue() == null) || (((Integer) form.getjSpnHours().getValue()).intValue() <= 0)) {
            return "Debe ingresar la cantidad de horas";
        }
        if (!this.isEditing()) {
            try {
                Sport sport = (Sport) form.getCmbSport().getSelectedItem();
                if (this.facadeDB.getSportByPatientBroker().existSportByPatient(sport, this.patient)) {
                    return "Ya existe la actividad física para el paciente";
                }
            } catch (ExceptionDAO ex) {
                this.facadeDB.refreshSession();
                return ex.getMessage();
            }
        }
        return null;
    }

    @Override
    protected void loadIconOnForm() {
    }

    @Override
    protected Object createEntity() throws ExceptionDAO {
        return new SportPatient(this.patient);
    }

    @Override
    protected JDialog createEditionForm(Frame parent) {
        return new JDialogSportByPatient(parent, this);
    }

    @Override
    protected void initFormsControls() throws ExceptionDAO {

        JDialogSportByPatient form = (JDialogSportByPatient) this.editForm;

        form.getCmbSport().removeAllItems();
        List<Sport> list = this.facadeDB.getSportBroker().getAllSports();
        Iterator<Sport> it = list.iterator();
        while (it.hasNext()) {
            form.getCmbSport().addItem(it.next());
        }
        form.getCmbSport().setEnabled(!this.isEditing());
        this.editForm = form;
    }
}
